package com.woniuxy.commons.config;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.commons.utils.BeanCopyUtil;
import com.woniuxy.commons.utils.OrderLogUtil;
import com.woniuxy.controller.form.CommentCreatForm;
import com.woniuxy.dao.model.CommentInfo;
import com.woniuxy.dao.model.DietOrder;
import com.woniuxy.service.CommentInfoService;
import com.woniuxy.service.DietOrderService;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Set;

@Configuration
@EnableScheduling
public class TimeListenUtil {
    @Resource
    private StringRedisTemplate srt;
    @Resource
    private RedisTemplate<String,Object> sot;
    @Resource
    private CommentInfoService commentInfoService;
    @Resource
    private DietOrderService dietOrderService;
    @Scheduled(cron = "1 5 6 * * ?")
    public void time(){
        Set<String> keys = sot.keys("*DD*");
        for (String key : keys) {
            System.out.println(key);
            Object o = sot.opsForValue().get(key);
            String orderNum = key.substring(key.lastIndexOf("D") + 1, key.length());
            if(null==o){
                DietOrder dietOrder = dietOrderService.getById(orderNum);
                if(dietOrder.getEvaluate()==0){
                    String d = key.substring(0, key.indexOf("D"));
                    Long storeId= Long.parseLong(d);
                    CommentCreatForm commentCreatForm = OrderLogUtil.creatComment(storeId, orderNum, 5, 5, 5, "好评",
                            0, "空","系统","系统默认好评");
                    CommentInfo commentInfo = BeanCopyUtil.copyOne(commentCreatForm, CommentInfo.class);
                    commentInfoService.save(commentInfo);
                    //修改订单状态为已评价
                    boolean update = dietOrderService.update(new UpdateWrapper<DietOrder>().eq("orderNum", orderNum).set("evaluate", 1));
                }
            }
        }
    }
}
